package com.hue.utils;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
    获取RabbitMQ的连接的工具类
 */
public class ConnectionUtils {
    public static Connection getConnection() throws IOException, TimeoutException {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置连接主机和端口,默认为localhost和5672
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        //设置虚拟主机,默认为/
        connectionFactory.setVirtualHost("/demo01");
        //设置用名和密码，默认为guest和guest
        connectionFactory.setUsername("yfq");
        connectionFactory.setPassword("123456");

        //2.获取连接
        Connection connection = connectionFactory.newConnection();
        return connection;
    }
}
